package com.hyt.it.ogt.kq.service.bm.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.enums.DelFlagEnum;
import com.hyt.it.ogt.kq.service.bm.mapper.ProjectSubjectAreaLimitMapper;
import com.hyt.it.ogt.kq.service.bm.model.entity.ProjectSubjectAreaLimit;
import com.hyt.it.ogt.kq.service.bm.model.entity.StudentInfo;
import com.hyt.it.ogt.kq.service.bm.model.entity.StudentSubject;
import com.hyt.it.ogt.kq.service.bm.service.IProjectSubjectAreaLimitService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentInfoService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentSubjectService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;


/**
 * <p>
 * 报名项目科目区域额度限制 服务实现类
 * </p>
 *
 * @author huangwu
 * @since 2023-08-18
 */
@Slf4j
@Service
public class ProjectSubjectAreaLimitServiceImpl extends BaseServiceImpl<ProjectSubjectAreaLimitMapper, ProjectSubjectAreaLimit> implements IProjectSubjectAreaLimitService {

	@Resource
	private ProjectSubjectAreaLimitMapper projectSubjectAreaLimitMapper;
	
	@Resource
    private IStudentSubjectService studentSubjectService;
	
	@Resource
    private IStudentInfoService studentInfoService;
	

    /**
     * 检查是否超额
     * @see com.hyt.it.ogt.kq.service.bm.service.IProjectSubjectAreaLimitService#checkExcessBySubjectIdAndArea(java.lang.String, java.lang.String)
     **/
    @Override
    public Boolean checkExcessBySubjectIdAndArea(String projectId, String studentId, String subjectId, String bmAreaCollId, String bmArea) {
        log.info("# checkExcessBySubjectIdAndArea subjectId:{},bmArea:{}", subjectId, bmArea);
        if(StrUtil.isEmptyIfStr(bmArea) || StrUtil.isEmptyIfStr(subjectId)) {
            return Boolean.FALSE;
        }
        List<String> dIds =  StrUtil.split(bmArea, ",");
        String lastAreaId = CollUtil.getLast(dIds);
        // 新查询报考报考该区域的考生
        // 首先查询科目的报考人数
        QueryWrapper<StudentInfo> studentInfoQueryWrapper = new QueryWrapper<>();
        studentInfoQueryWrapper.lambda()
                .select(StudentInfo::getStudentId)
                .eq(CharSequenceUtil.isNotEmpty(projectId), StudentInfo::getProjectId, projectId)
                .eq(CharSequenceUtil.isNotEmpty(bmAreaCollId), StudentInfo::getProjectCollectInfoId, bmAreaCollId)
                .eq(CharSequenceUtil.isNotEmpty(bmArea), StudentInfo::getValue, bmArea)
                .eq(StudentInfo::getDelFlag, DelFlagEnum.NORMAL.getCode());
        List<StudentInfo> studentInfos = studentInfoService.list(studentInfoQueryWrapper);
        List<String> studentIds = new ArrayList<>();
        if(CollUtil.isNotEmpty(studentInfos)) {
            studentIds = studentInfos.stream().map(StudentInfo::getStudentId).collect(Collectors.toList());
        }
        studentIds.add("-999999999");
        if(CollUtil.isNotEmpty(studentIds) && StrUtil.isNotBlank(studentId)) {
            for (Iterator<String> iterator = studentIds.iterator(); iterator.hasNext();) {
                String studentIdTemp =  iterator.next();
                if(studentIdTemp.equals(studentId)) {
                    iterator.remove();
                }
            }
        }
        
        
        // 首先查询科目的报考人数
        QueryWrapper<StudentSubject> studentSubjectQueryWrapper = new QueryWrapper<>();
        studentSubjectQueryWrapper.lambda()
                .eq(StudentSubject::getSubjectId, subjectId)
                .in(CollUtil.isNotEmpty(studentIds), StudentSubject::getStudentId, studentIds);
        int dbStudentSubjectCount =  studentSubjectService.count(studentSubjectQueryWrapper);
        
        
        // 查询科目区域配置的额度
        // 首先查询科目的报考人数
        QueryWrapper<ProjectSubjectAreaLimit> projectSubjectAreaLimitQueryWrapper = new QueryWrapper<>();
        projectSubjectAreaLimitQueryWrapper.lambda()
                .eq(ProjectSubjectAreaLimit::getSubjectId, subjectId)
                .eq(CharSequenceUtil.isNotEmpty(lastAreaId), ProjectSubjectAreaLimit::getBmArea, lastAreaId);
        
        // 
        List<ProjectSubjectAreaLimit> projectSubjectAreaLimits = list(projectSubjectAreaLimitQueryWrapper);
        if(CollUtil.isEmpty(projectSubjectAreaLimits)) {
            return Boolean.FALSE;
        }
        int configCount = 0;
        for (ProjectSubjectAreaLimit projectSubjectAreaLimit : projectSubjectAreaLimits) {
            if(CharSequenceUtil.isNotEmpty(projectSubjectAreaLimit.getBmArea())) {
                configCount += projectSubjectAreaLimit.getBmLimit();
            }
        }
        
        // 如果数据库已经报考的科目数没有超出配置的额度
        if(dbStudentSubjectCount < configCount ) {
            return  Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * 查询区域限额配置
     * @see com.hyt.it.ogt.kq.service.bm.service.IProjectSubjectAreaLimitService#getLimit(java.lang.String, java.lang.String, java.lang.String)
     **/
    @Override
    public ProjectSubjectAreaLimit getLimit(String projectId, String subjectId, String bmAreaId) {
        // 查询科目区域配置的额度
        QueryWrapper<ProjectSubjectAreaLimit> projectSubjectAreaLimitQueryWrapper = new QueryWrapper<>();
        projectSubjectAreaLimitQueryWrapper.lambda()
                .eq(ProjectSubjectAreaLimit::getProjectId, projectId)
                .eq(ProjectSubjectAreaLimit::getSubjectId, subjectId)
                .eq(ProjectSubjectAreaLimit::getBmArea, bmAreaId)
                .eq(ProjectSubjectAreaLimit::getDelFlag, Boolean.FALSE).last("LIMIT 1");
        
        return getOne(projectSubjectAreaLimitQueryWrapper);
        
        
    }

    /**
     * 校验报名项目科目是否配置区域限额
     * @see com.hyt.it.ogt.kq.service.bm.service.IProjectSubjectAreaLimitService#checkSubjectConfigLimit(java.lang.String, java.lang.String)
     **/
    @Override
    public Boolean checkSubjectConfigLimit(String projectId, String subjectId) {
        // 查询科目区域配置的额度
        QueryWrapper<ProjectSubjectAreaLimit> projectSubjectAreaLimitQueryWrapper = new QueryWrapper<>();
        projectSubjectAreaLimitQueryWrapper.lambda()
                .eq(ProjectSubjectAreaLimit::getProjectId, projectId)
                .eq(ProjectSubjectAreaLimit::getSubjectId, subjectId)
                .eq(ProjectSubjectAreaLimit::getDelFlag, DelFlagEnum.NORMAL.getCode());
        return count(projectSubjectAreaLimitQueryWrapper) > 0 ? Boolean.TRUE : Boolean.FALSE;
    }
	
}
